<template>
    <div class="app-container">

        <!--  搜索栏  -->
        <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true" label-width="100px">
            <el-form-item label="业务域名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入业务域名" clearable size="small"/>
            </el-form-item>
            <el-form-item style="margin-left: 10px;">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" style="margin-left: 10px;" @click="resetQuery">重置</el-button>
        </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleBusiness">新增</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="businessData.slice((currentPage-1)*pageSize,currentPage*pageSize)" max-height="480px">
            <el-table-column align="center" type="index" label="序号"></el-table-column>
            <el-table-column label="业务域名" align="center" prop="name"/>
            <el-table-column label="编号" align="center" prop="code" />
            <el-table-column label="创建时间" align="center" prop="creationDate" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.creationDate) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" width="140" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit"
                            @click="handleEdit(scope.row)"
                    >编辑</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete"
                            @click="onDelete(scope.row.id)"
                    >删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
                v-show="businessData.length>0"
                :total="businessData.length"
                :page.sync="currentPage"
                :limit.sync="pageSize"
        />

        <!-- 添加业务对象 -->
        <el-dialog :title="title" :visible.sync="open" v-if="open" width="30%" append-to-body>
            <business-form :businessForm="businessForm" @submitForm="getList"
                               @cancelDialog="cancel"></business-form>
        </el-dialog>

    </div>
</template>

<script>
    import {searchmain, deletemain} from '@/api/basicdata/basictype'
    import businessForm from "./form/businessForm";

    export default {
        name: "Filetab",
        components: {
            businessForm,
        },
        data() {
            return {
                // 遮罩层
                loading: false,
                // 显示搜索条件
                showSearch: true,
                // 业务对象表格数据
                businessData: [],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 分页
                currentPage: 1,
                currentIndex: '',
                pageSize: 10,
                // 查询参数
                queryParams: {
                    name: undefined,
                },
                //    业务对象表单
                businessForm: {
                    code: '',
                    name: '',
                    status: '1',
                    id: undefined
                },

            }
        },
        created() {
            //初始化业务对象信息
            this.getList();
        },
        methods: {
            // 查询业务对象
            getList() {
                this.loading = true
                searchmain({'searchParams': {'name': ''}}).then(res => {
                    this.businessData = res.responseBody
                    this.loading = false
                })
            },

            /** 搜索按钮操作 */
            handleQuery() {
                this.loading = true
                if (this.queryParams.name === undefined) {
                    this.getList()
                    this.loading = false
                } else {
                    const searchParams = {
                        searchParams: this.queryParams
                    }
                    searchmain(searchParams).then(res => {
                        this.businessData = res.responseBody
                        this.loading = false
                    })
                }
            },

            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },

            /** 新增业务对象按钮操作 */
            handleBusiness() {
                this.open = true;
                this.title = "添加业务对象";
            },
            /** 修改业务对象按钮操作 */
            handleEdit(row) {
                this.businessForm.id = row.id
                this.businessForm.code = row.code
                this.businessForm.name = row.name
                this.open = true;
                this.title = "编辑业务对象";
            },

            /** 删除按钮操作 */
            onDelete(id) {
                this.$confirm('业务对象较为基础，不建议进行删除！如需删除请点确定按键！?', "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }).then(function () {
                    return deletemain(id);
                }).then(res => {
                    if (res.retCode === '200') {
                        this.$message.success('删除成功')
                        this.getList();
                    } else {
                        // console.log(res)
                        this.$message.error('删除失败' + res.retCode)
                    }
                })
            },

            // 取消按钮
            cancel() {
                this.open = false;
                this.title = '';
                this.businessForm.id = undefined;
                this.businessForm.code = '';
                this.businessForm.name = '';
                this.businessForm.status = '1';
            },
        }
    }
</script>
